package persistence;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Hashtable;

import docks.Dock;
import utils.DataDownloader;
import utils.HTML;
import views.DialogLauncher;
import android.R;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DataBaseHelper extends SQLiteOpenHelper {

    private static Context ctx;
    private static final int VERSION = 1;
    private static final String TAG = "DataBaseHelper";
    private static final String DATABASE_CREATE = "CREATE TABLE docks ("
//	    + " " + "_id INTEGER PRIMARY KEY AUTOINCREMENT," 
	    + " " + "_id INTEGER PRIMARY KEY,"
	    + " " + "address TEXT," 
	    + " " + "freeslots INTEGER,"
	    + " " + "available INTEGER,"
	    + " " + "latitude REAL," 
	    + " " + "longitude REAL," 
	    + " " + "starred INTEGER" 
	    + ");";

    public DataBaseHelper(Context context, String name) {

	super(context, name, null, VERSION);
	ctx = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

	db.execSQL(DATABASE_CREATE);
	
	Log.v("CHIVATO", "Dins de onCreate");
	

	    Log.v("CHIVATO", "Ara anem a baixar els valors d'internet");

	    
	    try {
		
		int numRead = 0;
		StringBuffer fileData = new StringBuffer(1000);
		InputStream raw = ctx.getAssets().open("bicingdata.txt");//ctx.openFileInput("bicingdata.txt");
		Reader is = new BufferedReader(new InputStreamReader(raw, "UTF8"));
		String xml = "";
		char[] buf = new char[512];
		
		 Log.v("CHIVATO", "GOING TO READ THE FILE!");
		
		while ((numRead = is.read(buf)) > 0) {
		    
		    Log.v("CHIVATO", "READING " + numRead + " bytes!!");
		    fileData.append(buf, 0, numRead);
		}
		is.close();
		xml = fileData.toString();
		
		xmlparser.SaxParser sax = new xmlparser.SaxParser(xml);
		sax.parseBicingState();
		Hashtable<Integer, Dock> list = sax.getDockList();
		
//		DataDownloader dd  = new DataDownloader();
//		Hashtable<Integer, Dock> list = dd.download();
		ContentValues values;
		
		for (Dock d : list.values()) {

		    if (d.getIdDock() != 0) {

			Log.v("CHIVATO", "Afegint Dock: " + d.getIdDock()
				+ "!!!!!");
			
			values = new ContentValues();
			values.put("_id", d.getIdDock());
			values.put("address", d.getAddress());
			values.put("freeslots", d.getFreeSlots());
			values.put("available", d.getAvailableBikes());
			values.put("latitude", d.getLatitude());
			values.put("longitude", d.getLongitude());
			values.put("starred", 0);

			db.insert("docks", null, values);
			
		    }
		}

	    } catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }
	    
//	    DialogLauncher.dismissDialog();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
		+ newVersion + ", which will destroy all old data");
	db.execSQL("DROP TABLE IF EXISTS docks");
	onCreate(db);
    }

}
